<?php
/*
 *    检测一个远程文件是否存在.
 *
 *    @param string $file
 *
 *     @return bool
 */
if (!function_exists('my_file_exists')) {
    function my_file_exists($file)
    {
        //如果是远程文件
        $ch = curl_init($file);
        curl_setopt($ch, CURLOPT_HEADER, 1); //输出文件头信息
        curl_setopt($ch, CURLOPT_NOBODY, 1); //不输出文件主体
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //将curl_exec()返回到变量
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //设置超时
        $contents = curl_exec($ch); //获取远程文件信息

        return preg_match('|HTTP/1.1 200|', $contents) ? true : false; //如果状态不是200,就表示获取失败。
    }
}
/*
 * 判定是否手机号.
 *
 * @param string $phone
 *
 * @return bool
 */
if (!function_exists('isPhone')) {
    function isPhone($phone)
    {
        if (preg_match("/^1[345789]{1}\d{9}$/", $phone)) {
            return true;
        } else {
            return false;
        }
    }
}
/**
 * 返回成功请求
 *
 * @param string $data
 * @param string $msg
 * @param string $header
 * @param string $value
 * @return mixed
 */
function responseSuccess($data = '', $msg = '成功', $url = '', $header = 'Content-Type', $value = 'application/json')
{
    $msg           = is_array($msg) ? json_encode($msg) : json_encode([$msg ?: trans('response.success')]);
    $res['status'] = ['errorCode' => 0, 'msg' => trans($msg) ? trans($msg) : $msg];
    $res['data']   = $data;
    $res['url']    = $url;
    return response($content = $res, $status = 200)->header($header, $value);
}

/**
 * 返回错误的请求
 *
 * @param string $msg
 * @param int $code
 * @param string $data
 * @param string $header
 * @param string $value
 * @return mixed
 */
function responseWrong($msg = '失败', $data = '', $code = 1, $header = 'Content-Typeaa', $value = 'application/json')
{
    $msg           = is_array($msg) ? json_encode($msg) : json_encode([$msg ?: trans('response.fail')]);
    $res['status'] = ['errorCode' => $code, 'msg' => trans($msg) ? trans($msg) : $msg];
    $res['data']   = $data;
    return response($content = $res, $status = 200)->header($header, $value);
}

/**
 * 成功跳转
 * @param string $msg
 * @param string $route
 * @return \Illuminate\Http\RedirectResponse
 */
function respS($msg = '', $route = '')
{
    $msg = trans($msg) ? trans($msg) : trans('res.success');
    return $route ? redirect($route)->with('mes', $msg) : redirect()->back()->withInput()->with('mes', $msg);
}

/**
 * 失败跳转
 * @param string $msg
 * @param string $route
 * @return \Illuminate\Http\RedirectResponse
 */
function respF($msg = '', $route = '')
{
    $msg = trans($msg) ? trans($msg) : trans('res.fail');
    return $route ? redirect($route)->with('mes', $msg) : redirect()->back()->withInput()->with('mes', $msg);
}
/*
 * 生成一个token.
 *
 * @param string $token
 *
 * @return string
 */
if (!function_exists('getToken')) {
    function getToken($token = null)
    {
        if ($token) {
            return md5(time() . $token);
        } else {
            return md5(time());
        }
    }
}
/*
 * 生成订单号.
 *
 * @return string 返回一个类似于1223143438859683的订单号
 */
if (!function_exists('get_microtimes')) {
    function get_microtimes()
    {
        list($usec, $sec) = explode(' ', microtime());

        return date('mdHi', $sec) . substr($usec, 2, 4) . rand('1000', '9999');
    }
}
/*
 * 生成一个账号account.
 *
 * @return string
 */
if (!function_exists('getAccount')) {
    function getAccount()
    {
        return 'TLW_' . date('md', time()) . getCode(2, 2) . rand(1111, 9999);
    }
}
/*
 * 生成一个昵称.
 *
 * @return string
 */
if (!function_exists('getNickname')) {
    function getNickname()
    {
        return '团友' . getCode(4, 3) . '_' . rand(111111111, 999999999);
    }
}
/*
 * 生成一个用户标识符.
 *
 * @return string
 */
if (!function_exists('getSign')) {
    function getSign()
    {
        return substr(md5(time() . getCode(3, 10)), 12, 12);
    }
}
/*
 * 生成一个随机字符串.
 *
 * @param int $type   字符串类型
 * @param int $length 字符串长度
 *
 * @return string
 */

function getCode($type = 1, $length = 6)
{
    switch ($type) {
        case 1: //数字
            $str = str_pad(mt_rand(0, pow(10, $length) - 1), $length, '0', STR_PAD_BOTH);
            break;
        case 2: //字母
            $arr = range('a', 'z');
            shuffle($arr);
            $str = str_shuffle(substr(implode('', $arr), 0, $length));
            break;
        case 3: //数字字母混合
            $string = range('a', 'z');
            $number = range(0, 9);
            $array  = array_merge($string, $number);
            shuffle($array);
            $str = str_shuffle(substr(implode('', $array), 0, $length));
            break;
        case 4: //大写字母
            $arrS = range('A', 'Z');
            shuffle($arrS);
            $str = str_shuffle(substr(implode('', $arrS), 0, $length));
            break;
    }

    return $str;
}

/*
 * 根据短信类型返回短信模板
 *
 * @param int $type
 *
 * @return string
 */
if (!function_exists('getTemplate')) {
    function getTemplate($type)
    {
        switch ($type) {
            case 1: //登陆验证
                $template = '172825';
                break;
            case 2: //修改密码
                $template = '172824';
                break;
            case 3: //注册验证
                $template = '172823';
                break;
            case 4: //绑定银行卡
                $template = '172822';
                break;
            case 5: //商家确认
                $template = '172212';
                break;
            case 6: //修改支付密码
                $template = '172822';
                break;
            case 7: //更换手机号
                $template = '172822';
                break;
            default:
                $template = '172822';
                break;
        }

        return $template;
    }
}

/**
 * 生成一个随机头像
 */
if (!function_exists('randomHeadimg')) {
    function randomHeadimg()
    {
        return '/web/headimg/' . rand(1, 18) . '.png';
    }
}

/**
 * 生成一个订单号
 */
if (!function_exists('getOrderNum')) {
    function getOrderNum()
    {
        return date('Ymd', time()) . rand(pow(10, 9), pow(10, 10) - 1);
    }
}
